package jmine.tec.json;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
 * {@link JSONNumber} pode ser qualquer {@link Number}
 * 
 * @author takeshi
 */
public class JSONNumber extends Number implements JSON {

    private final BigDecimal number;

    /**
     * @param number Number
     */
    public JSONNumber(BigDecimal number) {
        super();
        this.number = number;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public byte byteValue() {
        return this.number.byteValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double doubleValue() {
        return this.number.doubleValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public float floatValue() {
        return this.number.floatValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int intValue() {
        return this.number.intValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long longValue() {
        return this.number.longValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public short shortValue() {
        return this.number.shortValue();
    }

    /**
     * @return BigDecimal
     */
    public BigDecimal bigDecimalValue() {
        return this.number;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.number.toString();
    }

    /**
     * {@inheritDoc}
     */
    public Map<String, JSON> getAllProperties() {
        return Collections.emptyMap();
    }

    /**
     * {@inheritDoc}
     */
    public JSON getProperty(String propertyName) {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Collection<String> getPropertyNames() {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    public Collection<JSON> getPropertyValues() {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    public int getSize() {
        return 0;
    }

    /**
     * {@inheritDoc}
     */
    public void setProperty(String propertyName, JSON value) {
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#hashCode()
     */

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.number == null) ? 0 : this.number.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        JSONNumber other = (JSONNumber) obj;
        if (this.number == null) {
            if (other.number != null) {
                return false;
            }
        } else if (!this.number.equals(other.number)) {
            return false;
        }
        return true;
    }

    /**
     * @return Number
     */
    public Number getNumber() {
        return this.number;
    }

    /**
     * {@inheritDoc}
     */
    public Object toJava() {
        return this.number;
    }

    /**
     * {@inheritDoc}
     */
    public Writer write(Writer writer) throws IOException {
        writer.write(this.number.toPlainString());
        return writer;
    }
}
